package www.lagou.app;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.junit.Test;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class TestFindPhone {
    public static QueryRunner qr;

    static {
        //使用DBUtils接口实现类QueryRunner 加载Druid连接池
        qr = new QueryRunner(DruidUtils.getDataSource());
    }
    /**
     * 查询价格高于2000元，生产日期是2019年之前的所有手机
     */
    @Test
    public void testFindPhoneBYPrice() throws SQLException {

        //需求1:  查询价格高于2000元，生产日期是2019年之前的所有手机
        String sql = "select * from phone where price > ? and prodate < ?";

        Object[] param = {2000, "2019"};
        List<Object[]> query = qr.query(sql, new ArrayListHandler(),param);
        System.out.println("价格高于2000元，生产日期是2019年之前的手机有");
        for (Object[] objects : query) {
            System.out.println(Arrays.toString(objects));
        }
    }

    @Test
    public void testFindPhoneByColor() throws SQLException {

        //需求2:  查询所有颜色是白色的手机信息

        String sql = "select * from phone where color = ?";

        List<Object[]> query = qr.query(sql, new ArrayListHandler(), "白色");
        System.out.println("所有颜色是白色的手机有:");
        for (Object[] objects : query) {
            System.out.println(Arrays.toString(objects));
        }
    }
}
